$(document).ready(function(){
	displayLang = $("#display-lang").val();
	$("#pwd").val("");
	$(".ftext").focus(function(){
		$(this).css("border-color","#256EB1");
	});
	$("#pwd").blur(function(){
		pwdCheck(this);
	});	
	$("#uid").blur(function(){
		nameCheck(this);
	});	
	
	$(".group-button.cancel").live("click",function(){
		if($("input[name=from]").length>0){
			var location = decodeURIComponent($("input[name=from]").val());
			window.location = location;
		}else{
			var location = "/sview/index.jsp";
			if(displayLang=="zh"){
				location +=("&lang=zh");
			}
			window.location = location;
		}
	});
	
	$(".group-button.confirm").bind("click",function(){
		var sgid=$(".para .body").find("input[name=group_select]:checked");
		if(sgid.length==0){
			 if(displayLang=="en")
				 $.messager.alert('Error','Please select an editing group!','error');
			 else if(displayLang=="zh")
				   $.messager.alert('错误','请选择一个编辑组!','error');
			 return;
		}
		var userName =$("#uid").val();
		var gid = sgid.val();
		if(gid!="0"){
			setGroupByParam(userName,gid);
		}
		$("#loginForm").submit();
	});
});	

function blurState(e){
	$(e).css("border-color","#BDC7D8");
}

function pwdCheck(e){
	var flag = true;
	blurState(e);
	var val =$(e).val();
	if(val==""){
		var msg =$(e).parent().find(".error-msg");
		var textEn = "Please enter the password";
		var textZhSimple ="请输入密码";
		if(displayLang=="en")
			msg.text(textEn);
		else if(displayLang=="zh")
			msg.text(textZhSimple);
		msg.show();
		$(e).css("border-color","red");
		flag = false;
	}
	if(!flag)
		return false;
	return true;
}

function nameCheck(e){
	var flag =true;
	blurState(e);
	var val =$(e).val();
	if(val==""){
		var msg =$(e).parent().find(".error-msg");
		var textEn = "Please enter a name";
		var textZhSimple ="请输入用户名";
		if(displayLang=="en")
			msg.text(textEn);
		else if(displayLang=="zh")
			msg.text(textZhSimple);
		msg.show();
		$(e).css("border-color","red");
		flag = false;
	}
	if(!flag)
		return false;
	return true;
}

function accountExistTest(user,pwd){
	var flag  = true;
	 var data={};
	 data["user"]=user;
	 data["pwd"] = pwd;
     data =  JSON.stringify(data);
	 data=("login="+encodeURIComponent(data));
	$.ajax({
	  url:"loginValidate",
	  async:false,
	  data:data,
	  dataType:"text",
	  type:"post" ,
	  success:function(text){
		  if(text=="invalidate"){
		     flag = false;		
		  }
	  }
	});
	return flag;
}
function validateCheck(){
    if(nameCheck($("#uid"))&&pwdCheck($("#pwd"))){
    	if(accountExistTest($("#uid").val(),$("#pwd").val())){
    		groupSelect($("#uid").val());
        }else{
    		var msg =$(".inval-message");
    		var textEn = "Username or password is not correct";
    		var textZhSimple ="用户名或密码错误";
    		if(displayLang=="en")
    			msg.text(textEn);
    		else if(displayLang=="zh")
    			msg.text(textZhSimple);
    		msg.show();
    	}
    }
	return false;	
}

function groupSelect(userName){
	var data = {};
	data["userName"] = userName;
	data = JSON.stringify(data);
	data = ("groupList=" + encodeURIComponent(data));
	$.ajax({
		url:"/sview/setting",
		async : false,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			if(result.length==0){
				$("#loginForm").submit();
			}else{
				$(".link").hide();
				$("#register_section").hide();
				$("#group_section").show();
				var body = $("#group_section").find(".body");
				for(i=0;i<result.length;i++){
					var group = result[i];
					var id = group.id;
					var name = group.groupName;
					var div = $("<div class='goption'></div>");
					var input= $("<input type='radio' name='group_select' />");
					input.val(id);
					var span = $("<span></span>");
					span.html(name);
					div.append(input);
					div.append(span);
					body.append(div);
				}
				var id =0;
				var name ;
				if(displayLang=="en"){
					name="None (Sign in without being a member of any group)";
				}else if(displayLang=="zh"){
					name="无（不以任何组成员身份登录）";
				}
				var div = $("<div class='goption none'></div>");
				var input= $("<input type='radio' name='group_select' checked/>");
				input.val(id);
				var span = $("<span></span>");
				span.html(name);
				div.append(input);
				div.append(span);
				body.prepend(div);
			}
		}
	});
}

function setGroupByParam(userName,groupID){
	var data = {};
	data["user"] = userName;
	data["groupID"] = groupID;
	data = JSON.stringify(data);
	data = ("groupset=" + encodeURIComponent(data));
	$.ajax({
		url:"/sview/setting",
		async : false,
		data : data,
		dataType : "json",
		type : "post"
	});
}
